package com.atguigu.system.service;

import com.atguigu.common.utils.BeanUtils;
import com.atguigu.common.utils.MD5;
import com.atguigu.model.system.SysRole;
import com.atguigu.model.system.SysUser;
import com.atguigu.model.system.SysUserRole;
import com.atguigu.model.vo.user.*;
import com.atguigu.system.constant.SysUserStatusEnum;
import com.atguigu.system.manager.SysRoleManager;
import com.atguigu.system.manager.SysUserManager;
import com.atguigu.system.manager.SysUserRoleManager;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * SysUserService
 * </p>
 *
 * @author flyduck
 * @since 2023/10/25
 */
@Service
public class SysUserServiceImpl {

    @Autowired
    private SysUserManager sysUserManager;
    @Autowired
    private SysRoleManager sysRoleManager;
    @Autowired
    private SysUserRoleManager sysUserRoleManager;

    /**
     * 条件分页查询
     * @param pageParam
     * @param sysUserQueryReqVo
     * @return
     */
    public IPage<SysUserQueryRespVo> selectPage(Page<SysUser> pageParam, SysUserQueryReqVo sysUserQueryReqVo) {
        Page<SysUserQueryRespVo> page = sysUserManager.selectPage(pageParam,sysUserQueryReqVo);
        return page;
    }

    /**
     * 添加用户
     * @param sysUserSaveReqVo
     * @return
     */
    public boolean save(SysUserSaveReqVo sysUserSaveReqVo) {
        SysUser sysUser = BeanUtils.toBean(sysUserSaveReqVo, SysUser.class);
        //输入密码进行加密
        sysUser.setPassword(MD5.encrypt(sysUser.getPassword()));
        sysUser.setStatus(SysUserStatusEnum.NORMAL.getCode());
        return sysUserManager.save(sysUser);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public SysUserQueryRespVo findUserById(Long id) {
        SysUser sysUser = sysUserManager.getById(id);
        SysUserQueryRespVo sysUserQueryRespVo = BeanUtils.toBean(sysUser, SysUserQueryRespVo.class);
        return sysUserQueryRespVo;
    }

    /**
     * 修改用户
     * @param sysUserUpdateReqVo
     * @return
     */
    public boolean updateUser(SysUserUpdateReqVo sysUserUpdateReqVo) {
        SysUser sysUser = BeanUtils.toBean(sysUserUpdateReqVo, SysUser.class);
        return sysUserManager.updateById(sysUser);
    }

    /**
     * 根据id逻辑删除用户
     * @param id
     * @return
     */
    public boolean removeById(Long id) {
        return sysUserManager.removeById(id);
    }

    /**
     * 更改用户状态
     * @param id
     * @param status
     */
    public void updateStatus(Long id, Integer status) {
        SysUser sysUser = sysUserManager.getById(id);
        sysUser.setStatus(status);
        sysUserManager.updateById(sysUser);
    }

    /**
     * 获取用户的角色数据
     * @param userId
     * @return
     */
    public SysUserAssignRoleQueryRespVo getRolesByUserId(Long userId) {
        SysUserAssignRoleQueryRespVo respVo = new SysUserAssignRoleQueryRespVo();
        //获取所有角色
        List<SysRole> allRoles = sysRoleManager.list();
        //根据用户id查询分配的角色
        List<SysUserRole> sysUserRoleList = sysUserRoleManager.findByUserId(userId);
        List<Long> roleIdList = sysUserRoleList.stream()
                .map(SysUserRole::getRoleId)
                .collect(Collectors.toList());

        respVo.setAllRoles(allRoles);
        respVo.setUserRoleIds(roleIdList);
        return respVo;
    }

    /**
     * 用户分配角色
     * @param reqVo
     */
    public void doAssign(SysUserAssignRoleReqVo reqVo) {
        //根据用户id删除之前的角色
        sysUserRoleManager.deleteByUserId(reqVo.getUserId());
        //获取角色id，添加用户角色关系表
        List<Long> roleIdList = reqVo.getRoleIdList();
        sysUserRoleManager.insertList(reqVo.getUserId(),roleIdList);
    }
}
